package tgc.edu.demo.jkxt.controller;

import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import tgc.edu.demo.jkxt.custom.AjaxResult;
import tgc.edu.demo.jkxt.custom.TablePageable;
import tgc.edu.demo.jkxt.entity.SysRole;
import tgc.edu.demo.jkxt.entity.Teacher;
import tgc.edu.demo.jkxt.form.TeacherForm;
import tgc.edu.demo.jkxt.service.TeacherService;

@RestController
@RequestMapping(value = "/teacher")
@CrossOrigin
public class TeacherController{
	@Autowired
	private TeacherService service;
	
	  @RequestMapping(value = "/tealist")	  
	  @PreAuthorize(value = "hasRole('ROLE_ADMIN')") 
	  public Object list() {
		  List<Teacher> list = service.findAll(); 
		  return new AjaxResult(list); 
	}
		  
	  @RequestMapping(value = "/page") 
	  public Object page(TablePageable pageable,TeacherForm form) { 
		  PageRequest pageRequest =pageable.buildPageRequest(); 
		  Specification<Teacher> spec = buildSpec(form);
	      Page<Teacher> page = service.findAll(spec, pageRequest); 
	      return new AjaxResult(AjaxResult.buildTableResult(page));
	 
	  }
	@SuppressWarnings("serial")
	public Specification<Teacher> buildSpec(TeacherForm form) {
		Specification<Teacher> spec=new Specification<Teacher>() {

			@Override
			public Predicate toPredicate(Root<Teacher> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				String search = form.getSearch();
				if(StringUtils.hasText(search)) {					
					  HashSet<Predicate> rules2=new HashSet<>();
					  rules2.add(cb.like(root.get("username"),"%"+search+"%"));
					  rules2.add(cb.like(root.get("name"),"%"+search+"%"));					  
					  rules.add(cb.or(rules2.toArray( new Predicate[rules2.size()])));
					  
				}
					return cb.and(rules.toArray(new Predicate[rules.size()]));
				}			
		};
		return spec;
	}
//	@RequestMapping(value = "/save")
//	@ResponseBody
//	public Object save(Teacher form) {
//		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
//		Teacher model=new Teacher();
//		Integer id = form.getId();
//		if(id!=null) {
//			model=service.findById(id);
//		}else {
//			model.getRoles().add(new SysRole(2));
//			
//		}
//		model.setName(form.getName());
//		model.setUsername(form.getUsername());
//		model.setPassword(encoder.encode(form.getPassword()));
//		form.setPassword(model.getPassword());
//		BeanUtils.copyProperties(form, model,"id");
//		service.save(model);
//	
//		 return new AjaxResult("数据保存成功");
//	}
	@RequestMapping(value = "/save")
	public Object save(TeacherForm form) {

		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		Teacher model=new Teacher();
		SysRole role=new SysRole();
		Integer id = form.getId();
		if(id!=null) {
			model=service.findById(id);
		}else {
			model.setPassword(encoder.encode("1234"));
		    model.getRoles().add(new SysRole(2));
		}
		BeanUtils.copyProperties(form, model,"id");

		
		Teacher result = service.save(model);
		return new AjaxResult(result);
	}

	
	
	@RequestMapping(value = "/delete")
	public Object delete(Integer id) {
		service.deleteById(id);
		return new AjaxResult("删除成功");
	}
}
